package com.ssm.order.exception;

import com.ssm.dto.CommonResult;
import com.ssm.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Priority;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult<Void> exceptionHandler(Exception e) {
        log.error("未知异常！原因是:", e);
        if (e instanceof ServiceException) {
            ServiceException exception = (ServiceException) e;
            return CommonResult.error(exception.getCode(), e.getMessage());
        } else {
            return CommonResult.error(-100, e.getMessage() );
        }
    }
}